package com.jqb.struts2lab01.action;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport {
	
	private static final long serialVersionUID = 1L;

	private File userfile;
	private String userfileContentType;
	private String userfileFileName;

	public String execute() {

		String realpath = ServletActionContext.getServletContext().getRealPath("/userupload");

		if (userfile != null) {
			File file = new File(realpath, userfileFileName);
			if (file.exists())
				file.mkdirs();
			try {
				/*0字节文件将无法被上传*/
				FileUtils.copyFile(userfile, file);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return SUCCESS;
	}

	public File getUserfile() {
		return userfile;
	}

	public void setUserfile(File userfile) {
		this.userfile = userfile;
	}

	public String getUserfileContentType() {
		return userfileContentType;
	}

	public void setUserfileContentType(String userfileContentType) {
		this.userfileContentType = userfileContentType;
	}

	public String getUserfileFileName() {
		return userfileFileName;
	}

	public void setUserfileFileName(String userfileFileName) {
		this.userfileFileName = userfileFileName;
	}

}
